package org.dubnation.org.service.impl;

import org.dubnation.base.excel.download.util.DownloadNeed;
import org.dubnation.base.excel.download.vo.DepartmentVO;
import org.dubnation.base.excel.download.vo.EmployeeVO;
import org.dubnation.base.excel.download.vo.ShopVO;
import org.dubnation.org.domain.Department;
import org.dubnation.org.mapper.DepartmentMapper;
import org.dubnation.org.service.IDepartmentService;
import org.dubnation.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-27
 */
@Service
@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired(required = false)
    private DepartmentMapper mapper;

    @Override
    public List<Department> queryParent() {
        return mapper.queryParent();
    }

    /**
     * 重写新增和修改方法，更改path
     * @param department
     */
    @Override
    public void insert(Department department) {
        //新增数据，获取主键
        mapper.insert(department);
        //判断是否有父级部门
        if (Objects.nonNull(department.getParent().getId())){
            //若有父级部门，再次判断该父部门是否还有上一层部门
            Department parent = mapper.queryById(department.getParent().getId());
            if (Objects.isNull(parent.getParent())){
                //新增的部门只有一层父部门
                department.setPath("/" + department.getParent().getId() + "/" + department.getId());
            }else {
                //新增部门有两层父部门
                department.setPath("/" + parent.getParent().getId() + "/" + department.getParent().getId() + "/" + department.getId());
            }
        }else {
            //没有父部门
            department.setPath("/" + department.getId());
        }
        //对该条数据进行修改，讲path加进去
        mapper.update(department);
    }

    /**
     * 重写修改方法，更改path
     * @param department
     */
    @Override
    public void update(Department department) {
        mapper.update(department);
        //判断更新数据是否有父部门
        if (StringUtils.isEmpty(department.getParent().getId())){
            //没有父部门
            department.setPath("/" + department.getId());
        }else {
            //有父部门，判断是否有上级部门
            Department parent = mapper.queryById(department.getParent().getId());
            if (StringUtils.isEmpty(parent.getParent())){
                //只有一层父部门
                department.setPath("/" + department.getParent().getId() + "/" + department.getId());
            }else {
                //有两级父部门
                department.setPath("/" + parent.getParent().getId() + "/" + department.getParent().getId() + "/" + department.getId());
            }
        }
        mapper.update(department);
    }

    /**
     * 获取导出文件所需要的部门数据
     * @return
     */
    @Override
    public List<DepartmentVO> queryDepInfo(DownloadNeed downloadNeed) {
        List<Department> departmentList =  mapper.getDepartment(downloadNeed);
        List<DepartmentVO> departmentVOList = new ArrayList<>(); //用来存储处理后得到的导出数据
        //对获取的数据库数据进行处理
        departmentList.forEach(department -> {
            if (department.getEmployees().size() > 0){
                //排除没有员工的部门
                DepartmentVO departmentVO = new DepartmentVO();
                departmentVO.setId(department.getId());
                departmentVO.setName(department.getName());
                departmentVO.setState(department.getState());
                if (department.getEmployees().size() == 0)
                    departmentVO.setEmployeeVOS(null);
                departmentVO.setEmployeeVOS(
                        department.getEmployees().stream().map(employee -> {
                            EmployeeVO employeeVO = new EmployeeVO();
                            employeeVO.setRealName(employee.getRealName());
                            employeeVO.setEmail(employee.getEmail());
                            employeeVO.setPhone(employee.getPhone());
                            employeeVO.setAge(employee.getAge());
                            employeeVO.setSex(employee.getSex());
                            employeeVO.setState(employee.getState());
                            return employeeVO;
                        }).collect(Collectors.toList())
                );
                if (Objects.isNull(department.getParent())){
                    departmentVO.setParent(null); //上级部门为空
                }else departmentVO.setParent(department.getParent().getName());
                if (department.getShops().size() == 0)
                    departmentVO.setShopVOS(null);
                departmentVO.setShopVOS(
                        department.getShops().stream().map(shop -> {
                            ShopVO shopVO = new ShopVO();
                            shopVO.setName(shop.getName());
                            shopVO.setTel(shop.getTel());
                            shopVO.setRegisterTime(shop.getRegisterTime());
                            shopVO.setState(shop.getState());
                            shopVO.setAddress(shop.getAddress());
                            shopVO.setLogo(shop.getLogo());
                            return shopVO;
                        }).collect(Collectors.toList())
                );
                if (department.getChildren().size() == 0){
                    departmentVO.setChildren(null);
                }else departmentVO.setChildren(DepartmentVO.getChildren(department.getChildren()));
                departmentVOList.add(departmentVO);
            }
        });
        return departmentVOList;
    }
}
